package com.itheima.cotroller;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.sercive.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
@RestController
@RequestMapping("/setmeal")
public class SetMealController{
    @Autowired
    private SetmealService setmealService;

    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal) {
        setmealService.add(setmeal);
        return Result.success();
    }

    @PutMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal) {
        setmealService.edit(setmeal);
        return Result.success();
    }

    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id) {
        Setmeal setmeal = setmealService.findById(id);
        return Result.success(setmeal);
    }

    @GetMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return Result.success(pageResult);
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        String filename = UUID.randomUUID() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        file.transferTo(new File("D:/upload/" + filename));
        return Result.success(filename);
    }
}
